using System.Collections.Generic;
using Game.XH;
using UnityEngine;

namespace Game.Component.Character
{
	public class Leg : CharacterComponent
	{
		public enum Dir
		{
			Left = 0,
			Right = 1
		}

		public enum State
		{
			Default = 0,
			ReadyToSit = 1,
			Sit = 2,
			LieLeft = 3,
			LieRight = 4
		}

		private const float POWER_FACTOR = 20f;

		private const float MOVE_SPEED = 240f;

		private const float DECELERATION = 20f;

		private const float LIE_LEFT_UPPER_ANGLE = 20f;

		private const float LIE_LEFT_LOWER_ANGLE = 30f;

		public Dir dir;

		public State state;

		public List<BaseItem> targetList = new List<BaseItem>();

		public bool isHideShoes;

		public int clothingId;

		public int shoesId;

		public int bodyId;

		private Transform upperLeg;

		private Transform upperLegClothing;

		public Transform lowerLeg;

		public Transform lowerLegClothing;

		private Transform shoe;

		private float eachUpAngle;

		private float upAngle;

		private float eachDownAngle;

		private float downAngle;

		private float upperAngle;

		private float normalUpperAngle;

		private float minUpperAngle = -30f;

		private float maxUpperAngle = 30f;

		public Leg(Transform trans, Dir dir)
			: base(trans)
		{
			this.dir = dir;
			upperAngle = normalUpperAngle;
			upperLeg = trans.Find("UpperLeg");
			upperLeg.SetLocalZ(-8E-05f);
			upperLegClothing = trans.Find("UpperLeg/Clothing");
			upperLegClothing.SetLocalZ(-1E-05f);
			lowerLeg = trans.Find("UpperLeg/LowerLeg");
			lowerLeg.SetLocalZ(3E-05f);
			lowerLegClothing = trans.Find("UpperLeg/LowerLeg/Clothing");
			lowerLegClothing.SetLocalZ(-2E-05f);
			shoe = trans.Find("UpperLeg/LowerLeg/Shoe");
			shoe.SetLocalZ(-1E-05f);
		}

		public void SetBody(int id, Transform upperCopy, Transform lowerCopy, Color32 color)
		{
			if (bodyId != id)
			{
				bodyId = id;
				ChangeSprite(upperLeg, upperCopy);
				ChangeSprite(lowerLeg, lowerCopy);
				SetColor(color);
			}
		}

		public void SetColor(Color32 color)
		{
			ChangeSpriteColor(upperLeg, color);
			ChangeSpriteColor(lowerLeg, color);
		}

		public void SetBoxColliderEnabled(bool val)
		{
			BoxCollider2D component = transform.GetComponent<BoxCollider2D>();
			if (component != null)
			{
				component.enabled = val;
			}
		}

		public void SetLowerLegClothingVisible(bool val)
		{
			lowerLegClothing.SetObjectActive(val);
		}

		public void SetShoesVisible(bool val)
		{
			shoe.SetObjectActive(val);
		}

		public void IsHideShoes(bool val)
		{
			if (isHideShoes != val)
			{
				isHideShoes = val;
				shoe.Find("Sprite").SetObjectActive(!isHideShoes);
				shoe.Find("Sprite").SetObjectActive(!isHideShoes);
			}
		}

		public void SetClothing(int id, Transform upperCopy, Transform lowerCopy)
		{
			if (clothingId != id)
			{
				clothingId = id;
				ChangeSprite(upperLegClothing, upperCopy);
				ChangeSprite(lowerLegClothing, lowerCopy);
				if (clothingId == 34 || clothingId == 35)
				{
					IsHideShoes(true);
				}
				else
				{
					IsHideShoes(false);
				}
			}
		}

		public void SetShoes(int id, Transform copy)
		{
			if (shoesId != id)
			{
				shoesId = id;
				ChangeSprite(shoe, copy);
			}
		}

		public void AddTargetItem(BaseItem item)
		{
			if (!targetList.Contains(item))
			{
				targetList.Add(item);
			}
			if (targetList.Count > 0 && state != State.Sit)
			{
				ChangeState(State.ReadyToSit);
			}
		}

		public void RemoveTargetItem(BaseItem item)
		{
			targetList.Remove(item);
			if (targetList.Count <= 0 && state == State.ReadyToSit)
			{
				ChangeState(State.Default);
			}
		}

		public void ChangeState(State val)
		{
			if (state == val)
			{
				return;
			}
			state = val;
			if (state == State.Default)
			{
				upAngle = 0f;
				upperLeg.localEulerAngles = new Vector3(0f, 0f, 0f);
				lowerLeg.localEulerAngles = new Vector3(0f, 0f, 0f);
				lowerLeg.localScale = new Vector3(1f, 1f, 1f);
			}
			else if (state == State.ReadyToSit || state == State.Sit)
			{
				upperLeg.localEulerAngles = new Vector3(0f, 0f, 30f);
				lowerLeg.localEulerAngles = new Vector3(0f, 0f, -20f);
				lowerLeg.localScale = new Vector3(1f, 1f, 1f);
			}
			else if (state == State.LieLeft)
			{
				if (dir == Dir.Left)
				{
					upperLeg.localEulerAngles = new Vector3(0f, 0f, 20f);
					lowerLeg.localEulerAngles = new Vector3(0f, 0f, -30f);
				}
				else
				{
					upperLeg.localEulerAngles = new Vector3(0f, 0f, -20f);
					lowerLeg.localEulerAngles = new Vector3(0f, 0f, 30f);
					lowerLeg.localScale = new Vector3(-1f, 1f, 1f);
				}
			}
			else if (state == State.LieRight)
			{
				if (dir == Dir.Left)
				{
					upperLeg.localEulerAngles = new Vector3(0f, 0f, -20f);
					lowerLeg.localEulerAngles = new Vector3(0f, 0f, 30f);
					lowerLeg.localScale = new Vector3(-1f, 1f, 1f);
				}
				else
				{
					upperLeg.localEulerAngles = new Vector3(0f, 0f, 20f);
					lowerLeg.localEulerAngles = new Vector3(0f, 0f, -30f);
				}
			}
		}

		public void LateUpdate()
		{
			if (state != 0)
			{
				return;
			}
			if (downAngle > 0f)
			{
				eachDownAngle = 240f * Time.deltaTime;
				downAngle -= eachDownAngle;
				upperAngle -= eachDownAngle;
				if (upperAngle <= minUpperAngle)
				{
					upperAngle = minUpperAngle;
					upAngle = 0f;
					downAngle = 0f;
				}
				upperLeg.localEulerAngles = new Vector3(0f, 0f, upperAngle);
			}
			else if (upAngle > 0f)
			{
				eachUpAngle = 240f * Time.deltaTime;
				if (eachUpAngle > upAngle)
				{
					eachUpAngle = upAngle;
					upAngle = 0f;
				}
				else
				{
					upAngle -= eachUpAngle;
				}
				upperAngle += eachUpAngle;
				if (upperAngle >= maxUpperAngle)
				{
					upperAngle = maxUpperAngle;
					upAngle = 0f;
				}
				else if (upAngle <= 0f)
				{
					upAngle = 0f;
				}
				upperLeg.localEulerAngles = new Vector3(0f, 0f, upperAngle);
			}
			else if (upperAngle != normalUpperAngle)
			{
				if (Mathf.Abs(upperAngle - normalUpperAngle) < 0.3f)
				{
					upperAngle = normalUpperAngle;
				}
				else
				{
					upperAngle = Mathf.Lerp(upperAngle, normalUpperAngle, Time.deltaTime * 5f);
				}
				upperLeg.localEulerAngles = new Vector3(0f, 0f, upperAngle);
			}
		}

		public void SetTrigerEnabled(bool val)
		{
			Collider2D component = transform.GetComponent<Collider2D>();
			if (component != null)
			{
				component.isTrigger = val;
			}
		}

		public void AddUpPower(float val)
		{
			if (state == State.Default)
			{
				downAngle = 0f;
				upAngle += val * 20f;
			}
		}

		public void AddDownPower(float val)
		{
			if (state == State.Default)
			{
				upAngle = 0f;
				downAngle += val * 20f;
			}
		}
	}
}
